<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-4-11
 * Time: 下午12:01
 */
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class smscode_model extends MY_Model {
    protected $table = 'smscode';
    public function sendCode($phone,$type=1)
    {
        $now = time();
        $tmp = parent::get_one(array('phone'=>$phone,'type'=>$type, 'timeline >'=>$now - SMSCODE_RELOAD_TIME));

        if ($tmp) {
            $send_code_return['code'] = 421;
            $send_code_return['msg'] = '请再过'.(60 - ($now - $tmp['timeline'])).'秒请求短信';
            $send_code_return['phone'] = $phone;
            $send_code_return['auth_code'] = 0;
            return $send_code_return;
        }
        switch($type) {
            case 1:
                $ctype = 'bC0-rY0z4Kc8PqpB99vFm4';//注册
                break;
            case 2:
                $ctype = '9SpzzYfjkCJaUquZFKTguV';//密码找回
                break;
            case 3:
                $ctype = '2TVJRzFoQoLaFHU1cyJbi6';//修改密码
                break;
            case 6:
                $ctype = '2TVJRzFoQoLaFHU1cyJbi6';//修改密码
                break;

        }
        $send_code = mt_rand(100000, 999999);
        $info = json_decode(ry_sendCode($phone,$ctype));
        $code = $info->code;
        $msg = ry_sms_msg($info->code);
        $sessionId = '';
        if($code == 200) {
            $sessionId = $info->sessionId;
        }
        $data = array('phone'=>$phone,'type'=>$type, 'code'=>$code,'ip'=>get_ip(),'sessionId'=>$sessionId,'ry_msg'=>$msg,'timeline'=>time()+SMSCODE_EXPIRES_TIME);
        ry_sms_log($data);
        if ($this->create($data)) {
            return array('msg'=>$msg,'code'=>$code);
        } else {
            return false;
        }
        /*$send_info = send_authcode($phone,'',$ctype);
        if (!empty($send_info) && ($send_info['code'] == 0 || ENVIRONMENT == "development")) {

            $data = array('phone'=>$send_info['phone'],'type'=>$type, 'code'=>$send_info['auth_code'],'ip'=>get_ip());
            if ($this->create($data)) {

                return $send_info;
            } else {
                return false;
            }
        } else {

            return false;
        }*/
    }

    public function verifySmsCode($phone, $code, $type)
    {
        $now = time();
        $expired = SMSCODE_EXPIRES_TIME;
        $smscode = parent::get_one(array('phone'=>$phone, 'type'=>$type,'ip'=>get_ip(),'timeline >'=>$now - $expired));
        if ($smscode) {
            // 验证后删除短信验证码
            if (ENVIRONMENT != 'development') {
                $this->_delVerifySmsCode($phone);
            }
            $verify = json_decode(ry_verifyCode($smscode['sessionId'],$code));
            if($verify->success) {
                return true;
            } else {
                return false;
            }
        } else {
            if ((ENVIRONMENT == 'development') && ($code == '111111')) {
                return true;
            } else {
                return false;
            }
        }
    }

    public function _delVerifySmsCode($phone)
    {
        $where_del = array('phone'=>$phone);
        parent::del('*', $where_del);
    }
}
